using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;

using Impact.Controllers;
using Impact.Infrastructure.Exceptions;
using Impact.Views;

namespace Impact.Web.Foundation.Handlers
{
    /// <summary>
    /// HttpHandler for presenting Content on the view.  This handler intercepts all requests
    /// for a .ashx page and passes control to the ContentPresenter.
    /// </summary>
    /// <seealso cref="ContentPresenter"/>
    public class ContentHandler : IHttpHandler, IRequiresSessionState, IContentHandler
    {


        #region IHttpHandler Members

        /// <summary>
        /// Gets a value indicating whether another request can use this instance
        /// </summary>
        public bool IsReusable
        {
            get { return true; }
        }

        /// <summary>
        /// Processes the Http web request intercepted by this handler.
        /// Instantiates and initializes a ContentPresenter.
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                // this page is the page that handles hookup of the usercontrols with the master page.
                string pageName = "~/default.aspx";

                // compile the page
                Page page = (Page)PageParser.GetCompiledPageInstance(pageName, context.Server.MapPath(pageName), context);

                ContentPresenter presenter = null;
                if (page is IContentView)
                {
                    // attach the page to the presenter
                    presenter = new ContentPresenter(this, (IContentView)page);
                }
                else
                {
                    throw new ArgumentException("Configured page is not an IContentView");
                }


                if (presenter.RenderPage)
                {
                    // render the page
                    page.ProcessRequest(context);
                }


            }
            catch (Exception ex)
            {
                ExceptionManager.Handle(ex);
            }
        }
        #endregion

        #region IContentHandler Members

        /// <summary>
        /// The Url that made the request that this handler is serving
        /// </summary>
        public Uri RequestUrl
        {
            get { return HttpContext.Current.Request.Url; }
        }

        /// <summary>
        /// The QueryString passed in on the request
        /// </summary>
        public NameValueCollection QueryString
        {
            get { return HttpContext.Current.Request.QueryString; }
        }

        /// <summary>
        /// Gets the address of the user requesting the content
        /// </summary>
        public string UserHostAddress
        {
            get { return HttpContext.Current.Request.UserHostAddress; }
        }

        /// <summary>
        /// derive a standard string from a string encoded as a URL
        /// </summary>
        public string DecodeUrl(string url)
        {
            return HttpContext.Current.Server.UrlDecode(url);
        }

        /// <summary>
        /// redirects the request to a new location
        /// </summary>
        public void Redirect(string url)
        {
            HttpContext.Current.Response.Redirect(url, false);
        }

        /// <summary>
        /// render the content for the current request
        /// </summary>
        public void RenderRequest()
        {
            HttpContext context = HttpContext.Current;

            context.Response.Clear();

            // compile the page
            Page page = (Page)PageParser.GetCompiledPageInstance(context.Request.AppRelativeCurrentExecutionFilePath, 
                context.Request.PhysicalPath, context);

            // render the page
            page.ProcessRequest(context);

            context.Response.End();

        }


        #endregion
    }


}
